package com.wocharging.wxapp.filter;

import com.wocharging.base.domain.WoWxUser;
import com.wocharging.wxapp.config.JWTConfig;
import com.wocharging.wxapp.exception.NotLoginException;
import com.wocharging.wxapp.util.JwtUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FilterInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        Object token =  request.getHeader(JWTConfig.tokenHeader);
        if (token == null) {
            throw new NotLoginException("token不存在");
        }


        WoWxUser user = JwtUtil.parseAccessToken(token.toString());
        if (user == null) {
            throw new NotLoginException("token过期");
        }

        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
//        System.out.println("postHandle: " + request.getRequestURI());
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
//        System.out.println("afterCompletion: " + request.getRequestURI());
    }

}
